//
//  BigImageViewController.m
//  Product_NEWS
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "BigImageViewController.h"
#import "BigImageScrollView.h"
#import "UIImageView+WebCache.h"
#import "HeadLineViewController.h"
#import "UMSocialSnsPlatformManager.h"
#import "UMSocialSnsService.h"
@interface BigImageViewController ()

@end

@implementation BigImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    
}

-(void)initUI
{
    
    
    BigImageScrollView *bigImageSC = [[BigImageScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds] PictureModel:self.model];
    bigImageSC.contentSize = CGSizeMake(self.view.frame.size.width, self.model.height * (self.view.frame.size.width / self.model.width));
    [self.view addSubview:bigImageSC];
    bigImageSC.backgroundColor = [UIColor blackColor];
    
    //保存
    UIButton *save= [UIButton buttonWithType:UIButtonTypeCustom];
    save.layer.borderColor = [[UIColor colorWithWhite:0.576 alpha:1.000] CGColor];
    save.layer.borderWidth = 1.0f;
    
    save.frame = CGRectMake(310 * SCREEN_WIDTH, 620 *  SCREEN_HEIGHT, 50 * SCREEN_WIDTH, 30 * SCREEN_HEIGHT);
    [save setTitle:@"保存" forState:UIControlStateNormal];
    [save setTitleColor:[UIColor colorWithWhite:0.576 alpha:1.000] forState:UIControlStateNormal];
   save.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH];
    [save addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    save.alpha = 0.6;
    //    save.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:save];
    [save release];
    
    
    //转发
    UIButton *share= [UIButton buttonWithType:UIButtonTypeCustom];
    share.layer.borderColor = [[UIColor colorWithWhite:0.576 alpha:1.000] CGColor];
    share.layer.borderWidth = 1.0f;
    
    share.frame = CGRectMake(250 * SCREEN_WIDTH, 620 *  SCREEN_HEIGHT, 50 * SCREEN_WIDTH, 30 * SCREEN_HEIGHT);
    [share setTitle:@"转发" forState:UIControlStateNormal];
    [share setTitleColor:[UIColor colorWithWhite:0.576 alpha:1.000] forState:UIControlStateNormal];
    share.titleLabel.font = [UIFont systemFontOfSize:13 * SCREEN_WIDTH];
    share.alpha = 0.6;
    [share addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];
    //    save.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:share];
    [share release];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"iconfont-fanhui (2)"] forState:UIControlStateNormal];
    
    [back addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    back.frame = CGRectMake(15 * SCREEN_WIDTH, 10 * SCREEN_HEIGHT, 32* SCREEN_WIDTH, 32* SCREEN_HEIGHT);
    [self.view addSubview:back];
    [back release];
 
}

//保存功能
-(void)saveAction:(UIButton *)btn{
  
    UIImageView *imageView = [[UIImageView alloc] init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.model.image1] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        [self saveImageToPhotos:image];
        [image release];
        
    }];
 
}

- (void)saveImageToPhotos:(UIImage*)image
{
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
   
    if(error != NULL){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                                                        message:@"保存图片失败"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        
    }else{
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"保存图片成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [alertView show];
        // 设置定时器 自动消失
        [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(dismissAlert:) userInfo:[NSArray arrayWithObject:alertView] repeats:NO];
    }
    
}


// 实现定时器方法
- (void)dismissAlert:(NSTimer *)timer
{
    // 取到 alertView
    UIAlertView *alertView = [[timer userInfo] lastObject];
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
}



//分享
-(void)shareAction:(UIButton *)btn{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    /**
     Sns平台类，用`platformName`作为标识，指定显示名称、显示的图片，点击之后的响应。
     */
    snsPlatform.displayName = @"新浪";
    snsPlatform.loginName = @"网易";
    
    
    UMSocialSnsPlatform *snsPlatform2 = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    snsPlatform2.displayName = @"腾讯微博";
    
    
    
    //分享第二步
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"558936d367e58eb81b001298"
                                      shareText:@"你要分享的文字"
                                     shareImage:[UIImage imageNamed:@"Icon-29.png"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:nil];
    
}


-(void)backAction:(UIButton *)btn {
    
  
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = NO;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
